﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using EYSLibrary;
using EYayincilikSistemi ;

namespace EYayincilikSistemi
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EYayincilikDBEntities entities = new EYayincilikDBEntities();
            var query = from k in entities.Kullanici
                        from kt in entities.Kullanici_KullaniciTuru
                          where kt.KullaniciTuruId==1
                          && kt.KullaniciId==k.Id
                        select new {k.Soyad , k.Id};

            //+ ' '+k.Soyad
            cmbYazar.DataSource = query.ToList().AsQueryable();
            cmbYazar.DataTextField = "Soyad";
            cmbYazar.DataValueField="Id" ;


            EYayincilikDBEntities entities2 = new EYayincilikDBEntities();
            var query2  = from d in entities2.Dergi
                       
                        select new { d.DergiAdi, d.Id };

            //+ ' '+k.Soyad
            cmbDergi.DataSource = query2.ToList().AsQueryable();
            cmbDergi.DataTextField = "DergiAdi";
            cmbDergi.DataValueField = "Id";
            DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if(txtAnahtar.Text.Trim().ToString()!="")
            {
                    EYayincilikDBEntities entities = new EYayincilikDBEntities();
                    var query = from k in entities.Makale
                                from yy in entities.Kullanici 
                                where yy.Id==k.YazarId
                                && k.Metin.Contains(txtAnahtar.Text) 
                                select new { yy.Ad, k.MakaleId,k.Metin, k.Baslik};

                    
                    //query.AsQueryable().Contains<>
                    
                    GridView1.DataSource = query.ToList();
                    GridView1.DataBind();
            }

            else if (cmbDergi.SelectedIndex > 0 && cmbYazar.SelectedIndex > 0)
            {
                EYayincilikDBEntities entities = new EYayincilikDBEntities();

                var query = from k in entities.Makale
                            from yy in entities.Kullanici
                            from Dd in entities.Dergi
                            where yy.Id == k.YazarId
                            && Dd.Id==Convert.ToInt32(cmbDergi.SelectedValue)
                            && Dd.Id==k.DergiId
                            && yy.Id == Convert.ToInt32(cmbYazar.SelectedValue.ToString())
                            select new { yy.Ad, k.MakaleId, k.Metin, k.Baslik };


                //query.AsQueryable().Contains<>

                GridView1.DataSource = query.ToList();
                GridView1.DataBind();
            }
            //GridView1.Columns[0].HeaderText = " Yazar Adı";

            else if (cmbYazar.SelectedIndex > 0)
            {
                EYayincilikDBEntities entities = new EYayincilikDBEntities();
                var query = from k in entities.Makale
                            from yy in entities.Kullanici
                            where yy.Id == k.YazarId
                            && yy.Id == Convert.ToInt32(cmbYazar.SelectedValue.ToString())
                            select new { yy.Ad, k.MakaleId, k.Metin, k.Baslik };


                //query.AsQueryable().Contains<>

                GridView1.DataSource = query.ToList();
                GridView1.DataBind();
            }


         


            else if (cmbDergi.SelectedIndex > 0)
            {
                EYayincilikDBEntities entities = new EYayincilikDBEntities();
                var query = from k in entities.Makale
                            from yy in entities.Kullanici
                            from dd in entities.Dergi
                            where yy.Id == k.YazarId
                            && dd.Id==Convert.ToInt32( cmbDergi.SelectedValue.ToString())
                            && dd.Id==k.DergiId
                            select new { yy.Ad, k.MakaleId, k.Metin, k.Baslik };


                //query.AsQueryable().Contains<>

                GridView1.DataSource = query.ToList();
                GridView1.DataBind();
            }


            else if (cmbDergi.SelectedIndex > 0)
            {
                EYayincilikDBEntities entities = new EYayincilikDBEntities();
                var query = from k in entities.Makale
                            from yy in entities.Kullanici
                            where yy.Id == k.YazarId
                           && k.DergiId==Convert.ToInt32( cmbDergi.SelectedValue.ToString())
                            select new { yy.Ad, k.MakaleId, k.Metin, k.Baslik };


                //query.AsQueryable().Contains<>

                GridView1.DataSource = query.ToList();
                GridView1.DataBind();
            }   
                    //GridView1.Columns[0].HeaderText = " Yazar Adı";
        }
    }
}